//
//  TweetPhoto.java
//
//  Created by David J. Hinson on 2/6/10.
//  Copyright 2010 TweetPhoto, Inc. All rights reserved.
//
//	Java Implementation for the TweetPhoto API, as specified here: http://groups.google.com/group/tweetphoto/web

package com.tweetphoto.api;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.util.Log;

public class TweetPhoto {

	private static final String TAG = "TPAPI";

	String	m_APIKey         = null;
	Boolean	m_isOAuth        = false;
	String	m_ConsumerKey    = null;
	String	m_ConsumerSecret = null;
	int		m_StatusCode     = 0;
	String	m_LastError      = null;
	
	String	m_tokenIdentifier = null;
	String	m_tokenSecret     = null;
	String	m_tokenVerifier   = null;
	
	String	m_ServiceName     = "Twitter";
	
	int		m_httpStatus	  = 0;
	
	// Logged in user's id
	long	m_userId		  = 0;
	String	m_screenName	  = "";
	
	String describe() {
		String retVal = "APIKey: " + m_APIKey + "\nisOAuth: " + m_isOAuth + ",\nConsumer Key: " + m_ConsumerKey + ",\nConsumer Secret: " + m_ConsumerSecret;
		
		retVal += "Token Identifier: " + m_tokenIdentifier + ",\nToken Secret: " + m_tokenSecret + ",\nService Name: " + m_ServiceName;
		return retVal;
	}
	   
    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            
            if (authState.getAuthScheme() == null) {
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                Credentials creds = credsProvider.getCredentials(authScope);
                if (creds != null) {
                    authState.setAuthScheme(new BasicScheme());
                    authState.setCredentials(creds);
                }
            }
        }    
    };
    
	// Upload a photo to TweetPhoto
	TweetPhotoResponse uploadPhoto(FileEntity photo, String comment, String tags, double latitude, double longitude, String mimeType) {
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/upload2";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPost httpPost 				= new HttpPost(urlToRequest);
        
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPAPIKEY",  m_APIKey);
		httpPost.addHeader("TPPOST",    "True");
		httpPost.addHeader("TPMIMETYPE", mimeType);
		httpPost.addHeader("TPUTF8",    "True");
		httpPost.addHeader("TPMSG",     Base64Coder.encodeString(comment));
		
		if (latitude!=0.0 && longitude!=0.0) {
			httpPost.addHeader("TPLAT", "" +latitude);
			httpPost.addHeader("TPLONG", "" + longitude);
			}
		
		if (tags!=null && tags.length()>0) {
			httpPost.addHeader("TPTAGS", Base64Coder.encodeString(tags));
			}
		
		try {	        
			httpPost.setEntity(photo);
			
			HttpResponse httpResp = httpClient.execute(httpPost);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 201) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Upload Photo Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();				
				content.close(); 
				return response;
    			}

			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}
    
	// Delete a photo location
	TweetPhotoResponse deleteLocation(long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/location";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpDelete httpDelete 			= new HttpDelete(urlToRequest);
        
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPAPIKEY", m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpDelete);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Delete Location Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Set a photo location
	TweetPhotoResponse setLocation(long photoId, float latitude, float longitude) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/location";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			StringEntity entity = new StringEntity("" + latitude + "," + longitude);
			httpPut.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Set Location Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Set a map type
	Settings setMapType(long userId, int mapType) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/settings/maptype";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			StringEntity entity = new StringEntity("" + mapType);
			httpPut.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Set Map Type Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSettingsParser responseParser = new DomSettingsParser();
				responseParser.m_Xml = xml;
				Settings response = (Settings)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Set PIN
	Settings setPIN(long userId, int pin) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/settings/pin";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			StringEntity entity = new StringEntity("" + pin);
			httpPut.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Set PIN Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSettingsParser responseParser = new DomSettingsParser();
				responseParser.m_Xml = xml;
				Settings response = (Settings)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Set Shorten URL
	Settings setShortenUrl(long userId, int shortenUrl) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/settings/shortenurl";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			StringEntity entity = new StringEntity("" + shortenUrl);
			httpPut.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Set Shorten Url Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSettingsParser responseParser = new DomSettingsParser();
				responseParser.m_Xml = xml;
				Settings response = (Settings)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Set Do Not Tweet Favorite Photo
	Settings setDoNotTweetFavoritePhoto(long userId, int flag) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/settings/donottweetfavoritephoto";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			StringEntity entity = new StringEntity("" + flag);
			httpPut.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Set Do Not Tweet Favorite Photo Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSettingsParser responseParser = new DomSettingsParser();
				responseParser.m_Xml = xml;
				Settings response = (Settings)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Set Hide Viewing Patterns
	Settings setHideViewingPatterns(long userId, int flag) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/settings/hideviewingpatterns";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			StringEntity entity = new StringEntity("" + flag);
			httpPut.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Set Hide Viewing Patterns Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSettingsParser responseParser = new DomSettingsParser();
				responseParser.m_Xml = xml;
				Settings response = (Settings)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Set Hide Votes
	Settings setHideVotes(long userId, int flag) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/settings/hidevotes";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			StringEntity entity = new StringEntity("" + flag);
			httpPut.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Set Hide Votes Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSettingsParser responseParser = new DomSettingsParser();
				responseParser.m_Xml = xml;
				Settings response = (Settings)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Delete a photo
	TweetPhotoResponse deletePhoto(long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpDelete httpDelete 			= new HttpDelete(urlToRequest);
        
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPAPIKEY", m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpDelete);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Delete Photo Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// mark photo as "favorite" of a user
	TweetPhotoResponse favorite(long userId, long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/favorites/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPost httpPost 				= new HttpPost(urlToRequest);
        
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPAPIKEY", m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpPost);  
    	
			int status = httpResp.getStatusLine().getStatusCode();
   	
			if (status != 201) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Favorite Error: (" + status + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 				
				return response;
    			}

			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Delete a photo Favorite
	TweetPhotoResponse deleteFavorite(long userId, long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/favorites/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpDelete httpDelete 			= new HttpDelete(urlToRequest);
        
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPAPIKEY", m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpDelete);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Delete Favorite Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				Log.d(TAG, xml);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}

	// Post a comment
	AddPhotoCommentResponse comment(long userId, long photoId, String commentText) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/comments/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPost httpPost 				= new HttpPost(urlToRequest);
        
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPAPIKEY", m_APIKey);
		
		try {			
			httpPost.setEntity(new StringEntity(commentText));
	        
			HttpResponse httpResp = httpClient.execute(httpPost);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 201) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Add Photo Comment Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomAddCommentResponseParser responseParser = new DomAddCommentResponseParser();
				responseParser.m_Xml = xml;				
				AddPhotoCommentResponse response = (AddPhotoCommentResponse)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
    
	// Delete a photo Comment
	TweetPhotoResponse deleteComment(long userId, long photoId, long commentId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/comments/" + photoId + "/" + commentId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpDelete httpDelete 			= new HttpDelete(urlToRequest);
        
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPAPIKEY", m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpDelete);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Delete Comment Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 
				return response;
    			}

			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
	  
    // Get friends
    Profiles friends(long userId, int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/friends?ps=" + ps + "&ind=" + ind + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Friends Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomProfilesParser profilesParser = new DomProfilesParser();
				profilesParser.m_Xml = xml;
				Profiles profiles = profilesParser.parse();
				content.close(); 
				return profiles;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
	  
    // Link a service to the currently auth'd user
    TweetPhotoResponse linkService(String apiKey, String identityToken, String identitySecret, String serviceName) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/link";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPost httpPost 				= new HttpPost(urlToRequest);
        
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPost.addHeader("TPAPIKEY",  m_APIKey);
        
		String xmlBlob = "<LinkedProfile xmlns=\"http://tweetphotoapi.com\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><APIKey>" + apiKey + "</APIKey><IdentitySecret>" + identitySecret + "</IdentitySecret><IdentityToken>" + identityToken + "</IdentityToken><Service>" + serviceName + "</Service></LinkedProfile>";
		
		try {
			StringEntity entity = new StringEntity(xmlBlob);
			httpPost.setEntity(entity);
			
			HttpResponse httpResp = httpClient.execute(httpPost);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Linked Services Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }	  
	  
    // Un-Link a service connected to the currently auth'd user
    TweetPhotoResponse unlinkService(String serviceName) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/unlink/" + serviceName;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpDelete httpDelete 			= new HttpDelete(urlToRequest);
        
		httpDelete.addHeader("TPSERVICE", m_ServiceName);
		httpDelete.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpDelete.addHeader("TPAPIKEY",  m_APIKey);
		
		try {			
			HttpResponse httpResp = httpClient.execute(httpDelete);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Linked Services Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }	  
	  
    // Get linked services profiles
    Profiles linkedServices() {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/profiles";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		httpGet.addHeader("TPSERVICE", m_ServiceName);
		httpGet.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpGet.addHeader("TPAPIKEY",  m_APIKey);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Linked Services Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomProfilesParser profilesParser = new DomProfilesParser();
				profilesParser.m_Xml = xml;
				Profiles profiles = profilesParser.parse();
				content.close(); 
				return profiles;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }	  
    
    // Get all friends
    Profiles friends(long userId, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/friends?sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Friends Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomProfilesParser profilesParser = new DomProfilesParser();
				profilesParser.m_Xml = xml;
				Profiles profiles = profilesParser.parse();
				content.close(); 
				return profiles;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
	  
    // Get photo viewers
    Profiles photoViewers(long photoId, int ps, int ind) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/viewers?ps=" + ps + "&ind=" + ind + "&sort=desc";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Photo Viewers Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomProfilesParser profilesParser = new DomProfilesParser();
				profilesParser.m_Xml = xml;
				Profiles profiles = profilesParser.parse();
				content.close(); 
				return profiles;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
	
    // Get photo comments by user
    Comments photoCommentsByUser(long userId, int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/photos/comments?ps=" + ps + "&ind=" + ind + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Photo Comments By User Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomCommentsParser commentParser = new DomCommentsParser();
				commentParser.m_Xml = xml;				
				Comments comments = commentParser.parse();
				content.close(); 
				return comments;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
		
    // Get photo comments
    Comments photoComments(long photoId, int ps, int ind) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/comments?ps=" + ps + "&ind=" + ind + "&sort=desc";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Photo Comments Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomCommentsParser commentParser = new DomCommentsParser();
				commentParser.m_Xml = xml;				
				Comments comments = commentParser.parse();
				content.close(); 				
				return comments;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
    
    // Get user comments
    Comments userComments(long userId, int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/comments?ps=" + ps + "&ind=" + ind + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "User Comments Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomCommentsParser commentParser = new DomCommentsParser();
				commentParser.m_Xml = xml;				
				Comments comments = commentParser.parse();
				content.close(); 
				return comments;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
    
    // Get all user comments
    Comments userComments(long userId, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/comments?sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "User Comments Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomCommentsParser commentParser = new DomCommentsParser();
				commentParser.m_Xml = xml;				
				Comments comments = commentParser.parse();
				content.close(); 
				return comments;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
    
    // Get all photo comments
    Comments photoComments(long photoId, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/comments?sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "User Comments Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomCommentsParser commentParser = new DomCommentsParser();
				commentParser.m_Xml = xml;				
				Comments comments = commentParser.parse();
				content.close(); 
				return comments;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
    
    // Get photo comments
    Comments photoComments(long photoId, int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/comments?ps=" + ps + "&ind=" + ind + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "User Comments Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomCommentsParser commentParser = new DomCommentsParser();
				commentParser.m_Xml = xml;				
				Comments comments = commentParser.parse();
				content.close(); 
				return comments;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
	
    // Get favorites
    Favorites favorites(long userId, int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/favorites?ind=" + ind + "&ps=" + ps + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Favorites Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomFavoritesParser favoritesParser = new DomFavoritesParser();
				favoritesParser.m_Xml = xml;				
				Favorites favorites = favoritesParser.parse();
				content.close(); 
				return favorites;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
	
    // Get all favorites
    Favorites favorites(long userId, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/favorites?sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Favorites Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomFavoritesParser favoritesParser = new DomFavoritesParser();
				favoritesParser.m_Xml = xml;				
				Favorites favorites = favoritesParser.parse();
				content.close(); 
				return favorites;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
    }
    
	// Sign In to TweetPhoto
	Profile signIn(String apiKey, String serviceName, Boolean isOAuth, String userIdentity, String userSecret) {
		
		m_APIKey  			= apiKey;
		m_isOAuth 			= isOAuth;
		m_ServiceName 		= serviceName;
		m_tokenIdentifier	= userIdentity;
		m_tokenSecret		= userSecret;
		
		String urlToRequest 			= m_isOAuth ? "http://tweetphotoapi.com/api/tpapi.svc/oauthsignin" : "http://tweetphotoapi.com/api/tpapi.svc/signin";
		
		// Facebook connect
		if (m_ServiceName.equalsIgnoreCase("Facebook") ) {
			urlToRequest = "http://tweetphotoapi.com/api/tpapi.svc/facebooksignin";
			}
		
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		httpGet.addHeader("TPSERVICE", m_ServiceName);
		httpGet.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpGet.addHeader("TPAPIKEY",  m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Sign In Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomProfileParser profileParser = new DomProfileParser();
				profileParser.m_Xml = xml;
				Profile profile = (Profile)profileParser.parse();
				
				m_userId     = profile.m_id;
				m_screenName = profile.m_screenName;
				
				content.close(); 
				return profile;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}

	// Get the public feed (all)
	SocialFeed publicFeed(String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/socialfeed?sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Public Feed Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSocialFeedParser profileParser = new DomSocialFeedParser();
				profileParser.m_Xml = xml;
				SocialFeed feed = profileParser.parse();
				content.close(); 
				return feed;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get the social feed
	SocialFeed socialFeed(long userId, int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/feed?ps=" + ps + "&ind=" + ind + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Social Feed Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSocialFeedParser profileParser = new DomSocialFeedParser();
				profileParser.m_Xml = xml;
				SocialFeed feed = profileParser.parse();
				content.close(); 
				return feed;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get the public feed
	SocialFeed publicFeed(int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/socialfeed?ps=" + ps + "&ind=" + ind + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Public Feed Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSocialFeedParser profileParser = new DomSocialFeedParser();
				profileParser.m_Xml = xml;
				SocialFeed feed = profileParser.parse();
				content.close(); 
				return feed;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}
	
	// Get the social feed (friends)
	SocialFeed socialFeed(long userId) {
		return socialFeed(userId, 10, 0, "desc");
	}

	// Get Next Photo
	Photo next(long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/next";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Next Photo Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomPhotoParser photoParser = new DomPhotoParser();
				photoParser.m_Xml = xml;
				Photo photo = photoParser.parse();
				content.close(); 
				return photo;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get Previous Photo
	Photo previous(long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/previous";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Previous Photo Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomPhotoParser photoParser = new DomPhotoParser();
				photoParser.m_Xml = xml;
				Photo photo = photoParser.parse();
				content.close(); 
				return photo;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get Photo Metadata
	Photo photoMetaData(long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Photo Metadata Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomPhotoParser photoParser = new DomPhotoParser();
				photoParser.m_Xml = xml;
				Photo photo = photoParser.parse();
				content.close(); 
				return photo;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get the user profile
	Profile userProfile(long userId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK && m_httpStatus != 201) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "User Profile Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomProfileParser profileParser = new DomProfileParser();
				profileParser.m_Xml = xml;
				Profile profile = profileParser.parse();
				content.close(); 
				return profile;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get the user profile
	Profile userProfile(String userName) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userName;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK && m_httpStatus != 201) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "User Profile Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomProfileParser profileParser = new DomProfileParser();
				profileParser.m_Xml = xml;
				Profile profile = profileParser.parse();
				content.close(); 
				return profile;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get the user settings
	Settings settings(long userId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/settings";
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
        
		HttpGet httpGet 				= new HttpGet(urlToRequest);
		
		httpGet.addHeader("TPSERVICE", m_ServiceName);
		httpGet.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpGet.addHeader("TPSERVICE", m_ServiceName);
		httpGet.addHeader("TPAPIKEY", m_APIKey);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK && m_httpStatus != 201) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "User Settings Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomSettingsParser settingsParser = new DomSettingsParser();
				settingsParser.m_Xml = xml;
				Settings settings = settingsParser.parse();
				content.close(); 
				return settings;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}
    
	// mark photo as "viewed" by the user
	TweetPhotoResponse view(long userId, long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/views/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPost httpPost 				= new HttpPost(urlToRequest);
        
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPost.addHeader("TPSERVICE", m_ServiceName);
		httpPost.addHeader("TPAPIKEY", m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpPost);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 201) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "View Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}

	// vote for a photo
	TweetPhotoResponse vote(long photoId, String voteCast) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/" + voteCast;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		httpClient.addRequestInterceptor(preemptiveAuth, 0);
		httpClient.getCredentialsProvider().setCredentials(new AuthScope("tweetphotoapi.com", 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(m_tokenIdentifier, m_tokenSecret)); 
		HttpPut httpPut 				= new HttpPut(urlToRequest);
        
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPISOAUTH", m_isOAuth ? "True" : "False");
		httpPut.addHeader("TPSERVICE", m_ServiceName);
		httpPut.addHeader("TPAPIKEY", m_APIKey);

		try {
			HttpResponse httpResp = httpClient.execute(httpPut);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Vote Error: (" + m_httpStatus + ") Data: " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomTweetPhotoResponseParser responseParser = new DomTweetPhotoResponseParser();
				responseParser.m_Xml = xml;
				TweetPhotoResponse response = (TweetPhotoResponse)responseParser.parse();
				content.close(); 
				return response;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
		}
	
	// Get favorite status for a photo
	FavoriteQueryResponse favoriteStatus(long userId, long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/favorites/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != 200) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Query Favorite Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomFavoriteQueryResponseParser favoriteParser = new DomFavoriteQueryResponseParser();
				favoriteParser.m_Xml = xml;
				FavoriteQueryResponse favoriteStatus = favoriteParser.parse();
				content.close(); 
				return favoriteStatus;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get all photos
	Photos photos(long userId, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/photos?sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Get Photo Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomPhotosParser photoParser = new DomPhotosParser();
				photoParser.m_Xml = xml;
				Photos photos = photoParser.parse();
				content.close(); 
				return photos;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	// Get photos
	Photos photos(long userId, int ps, int ind, String sort) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/photos?ps=" + ps + "&ind=" + ind + "&sort=" + sort;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Get Photo Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomPhotosParser photoParser = new DomPhotosParser();
				photoParser.m_Xml = xml;
				Photos photos = photoParser.parse();
				content.close(); 
				return photos;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}
	
	// Get leaderboard (types are viewed, voted, or commented
	Photos leaderboard(String type) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/leaderboard/uploadedtoday/" + type;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Leaderboard Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomPhotosParser photoParser = new DomPhotosParser();
				photoParser.m_Xml = xml;
				Photos photos = photoParser.parse();
				content.close(); 
				return photos;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}
	
	// Get vote status for a photo
	VoteStatus voteStatus(long userId, long photoId) {
		
		String urlToRequest 			= "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/votes/" + photoId;
		DefaultHttpClient httpClient 	= new DefaultHttpClient();
		HttpGet httpGet 				= new HttpGet(urlToRequest);
        
		try {
			HttpResponse httpResp = httpClient.execute(httpGet);  
    	
			m_httpStatus = httpResp.getStatusLine().getStatusCode();
   	
			if (m_httpStatus != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				httpResp.getEntity().writeTo(ostream);
				Log.e(TAG, "Error (" + m_httpStatus + "): " + ostream.toString());
			} else {
				InputStream content = httpResp.getEntity().getContent();
				String xml = convertStreamToString(content);
				DomVoteStatusParser voteParser = new DomVoteStatusParser();
				voteParser.m_Xml = xml;
				VoteStatus voteStatus = voteParser.parse();
				content.close(); 
				return voteStatus;
    			}
			}
		catch (Exception ex)
    		{
			ex.printStackTrace();
    		}
    
		return null;
	}

	String convertStreamToString(InputStream is) throws IOException {
	/*
	* To convert the InputStream to String we use the BufferedReader.readLine()
	* method. We iterate until the BufferedReader return null which means
	* there's no more data to read. Each line will appended to a StringBuilder
	* and returned as String.
	*/
	if (is != null) {
	StringBuilder sb = new StringBuilder();
	String line;
	 
	try {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8192);
	while ((line = reader.readLine()) != null) {
	sb.append(line).append("\n");
	}
	} finally {
	is.close();
	}
		return sb.toString();
		} 
	else {        
		return "";
		}   
	}
 	
}
